/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "wingbot_usart.h"
#include "wingbot_tick.h"

extern USART_INFO USART1_Info;
extern USART_INFO USART2_Info;
extern USART_INFO USART3_Info;
extern USART_INFO USART4_Info;
extern USART_INFO USART5_Info;
extern uint32_t _wb_sys_tick; 

static __IO uint32_t TimingDelay;
static CanRxMsg can_rx_msg = {0};

extern bool usart1_data_ready(void);
extern bool usart2_data_ready(void);
extern bool usart3_data_ready(void);
extern bool usart4_data_ready(void);
extern bool usart5_data_ready(void);

/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
#ifndef RTE_CMSIS_RTOS_RTX
void SVC_Handler(void)
{
}
#endif

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
#ifndef RTE_CMSIS_RTOS_RTX
void PendSV_Handler(void)
{
}
#endif

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
#ifndef RTE_CMSIS_RTOS_RTX

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void delay_ms(__IO uint32_t nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0);
}


/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0)
  { 
    TimingDelay--;
  }
}

void SysTick_Handler(void)
{
    _wb_sys_tick++;
    TimingDelay_Decrement();
    if(USART1_Info.trans_info.rx_state == TRANS_WAITING)
    {
        if(usart1_data_ready()){
            USART1_Info.trans_info.rx_dataReady = true;
            USART1_Info.trans_info.rx_state = TRANS_IDLE;
        }
    }
    if(USART2_Info.trans_info.rx_state == TRANS_WAITING)
    {
        if(usart2_data_ready()){
            USART2_Info.trans_info.rx_dataReady = true;
            USART2_Info.trans_info.rx_state = TRANS_IDLE;
        }
    }
    if(USART3_Info.trans_info.rx_state == TRANS_WAITING)
    {
        if(usart3_data_ready()){
            USART3_Info.trans_info.rx_dataReady = true;
            USART3_Info.trans_info.rx_state = TRANS_IDLE;
        }
    }
    if(USART4_Info.trans_info.rx_state == TRANS_WAITING)
    {
        if(usart4_data_ready()){
            USART4_Info.trans_info.rx_dataReady = true;
            USART4_Info.trans_info.rx_state = TRANS_IDLE;
        }
    }
    if(USART5_Info.trans_info.rx_state == TRANS_WAITING)
    {
        if(usart5_data_ready()){
            USART5_Info.trans_info.rx_dataReady = true;
            USART5_Info.trans_info.rx_state = TRANS_IDLE;
        }
    }
}
#endif
/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles USART1 interrupt request.
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
    USART1_Info.irq_handler();
}

void USART2_IRQHandler(void)
{
    USART2_Info.irq_handler();
}

void USART3_IRQHandler(void)
{
    USART3_Info.irq_handler();
}

void UART4_IRQHandler(void)
{
    USART4_Info.irq_handler();
}

void UART5_IRQHandler(void)
{
    USART5_Info.irq_handler();
}

void TIM2_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        wing_timer_tick(TIM2);
    }
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}

void TIM3_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {
        time_flags.ms10 = 1;
    }
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}

void TIM4_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
    {
        time_flags.ms100 = 1;
    }
    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}

/**
  * @brief CAN接收中断
  */
void USB_LP_CAN1_RX0_IRQHandler(void)
{
    int len = 0;
    CanRxMsg rx;
    CAN_Receive(CAN1, CAN_FIFO0, &rx);
    len = rx.DLC;
    if(len==8)
    {   
        len--;
    }
    else
    {
        len++;
    }
}

/**
  * @}
  */ 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
